<template>
  <div class="update-name">
    <van-nav-bar
      title="更新昵称"
      left-text="返回"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onComfirm"
    />
    <div class="name-field-wrap">
      <van-field
        v-model="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="50"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'

export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      required: true
    },
    abc: {
      type: Number
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  mounted () {

  },
  methods: {
    async onComfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidclick: true
      })
      try {
        await updateUserProfile({
          name: this.localName
        })
        this.$toast.success('保存成功')
        this.$emit('input', this.localName)
        // this.$emit('update-name', this.localName)
        this.$emit('close')
      } catch (err) {
        if (err & err.response && err.response.status === 400) {
          this.$toast.fail('昵称已存在')
        }
      }
    }
  }
}
</script>

<style lang="less">
  .name-field-wrap {
    padding: 10px;
  }
</style>
